// 首先封装节点
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

// 封装链表
class LinkedList {
  constructor() {
    // head是个空的节点元素
    this.head = null;
    this.length = 0;
  }

  // 给链表尾部添加一个新的项
  append(data){
    const newNode = new Node(data);
    //2. 追加到新节点
    if (this.length === 0) {  //空链表
      this.head = newNode;
    } else {
      let current = this.head;
      while(current.next != null){
        current = current.next;
      }
      current.next = newNode;
    }
    this.length++;
  }

  //在列表某位置插入某元素
  insert(position, data){
    // 1. 首先判断越界问题
    if (position < 0 || position > this.length) return false;
    // 2.创建新的节点
    const newNode = new Node(data);
    // 3.插入元素
    if (position === 0) {
      newNode.next = this.head;
      this.head = newNode;
    } else {
      let index = 0;
      let current = this.head;
      let previous = null;
      while(index++ < position) {
        previous = current;
        // 已经指向要推后的节点元素
        current = current.next
      }
      previous.next = newNode;
      newNode.next = current;
    }
    this.length++;
    return true
  }

  // 获取对应位置元素
  get(position){
    // 1. 首先判断越界问题
    if (position < 0 || position > this.length -1 ) return false;
    // 2. 查找该位置的元素
    let index = 0;
    let current = this.head;
    while(index++ < position){
      current = current.next;
    }
    return current.data
  }

  // 返回元素在列表中的索引
  indexOf(data) {
    let current = this.head;
    let index = 0;
    while(current) {
      if(current.data === data) {
        return index;
      }
      index++;
      current = current.next;
    }
    return -1;
  }

  //删除指定位置元素
  remove(position) {
    // 1. 首先判断越界问题
    if (position < 0 || position > this.length -1 ) return null;
    // 2.删除元素
    let current = this.head;
    let previous = null;
    let index = 0;
    if (position === 0) {
      this.head = current.next;
    } else {
      while (index++ < position){
        previous = current;
        current = current.next;
      }
      previous.next = current.next;
    }
    this.length--;
  }

  //修改某个位置的元素
  update(position, data){
    // 1. 首先判断越界问题
    if (position < 0 || position > this.length -1 ) return false;
    // 2.查找位置并修改data
    let current = this.head;
    let index = 0;
    while (index++ < position){
      current = current.next;
    }
    current.data = data
  }

  //给你data你去找他并且删掉
  removeData(data){
    // 获取某元素位置
    const index = this.indexOf(data);
    // 删除某个位置
    if (index === -1) return;
    this.remove(index);
  }

  //是否为空
  isEmpty() {
    return this.length === 0;
  }

  // 包含元素个数
  size() {
    return this.length;
  }
}


const linkedList = new LinkedList();
// 测试 append 方法
linkedList.append("A");
linkedList.append("B");
linkedList.append("C");
console.log(linkedList);
linkedList.insert(1,"0")
console.log(linkedList);
console.log(linkedList.get(2));
linkedList.remove(0);
console.log(linkedList);
linkedList.update(0,1);
console.log(linkedList);